<template>
    <!-- 对话框表单 -->
    <div
        class="ba-operate-form"
        :class="'ba-' + baTable.form.operate + '-form'"
        :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
    >
        <div style="text-align:right" v-if="baTable.form.items&&type==2&&baTable.form.operate !=='add'">
            <el-button type="primary" v-if="baTable.form.items.status=='1'" @click="handleTj">提交审批</el-button>
            <el-button type="primary" v-if="baTable.form.items.status=='2'" @click="editShow = true">修改申请</el-button>
            <el-button type="primary" disabled v-if="baTable.form.items.status=='0'">审批中</el-button>
        </div>
        <div class="form-wrapper">
            <div class="lock-btn" :class="{supplier:type==2}">
                <el-form-item label="锁定" label-width="60px" >
                    <el-switch
                        :disabled="type==2"
                        v-model="baTable.form.items!.LOCK"
                        style="--el-switch-on-color: #ff4949"
                        active-value="1"
                        inactive-value="0"
                    />
                </el-form-item>
            </div>
            <el-form
                v-if="!baTable.form.loading"
                ref="formRef"
                @keyup.enter="baTable.onSubmit(formRef)"
                :model="baTable.form.items"
                label-position="right"
                :label-width="'120px'"
                :disabled="baTable.form.operate=='view' || baTable.form.items!.LOCK=='1'"
                :rules="rules"
            >
                <div class="status-wrapper">
                    <el-form-item label="有效性" v-if="type == 1">
                        <el-radio-group v-model="baTable.form.items!.ACTIVE">
                            <el-radio-button label="0" color="#626aef" type="danger">无效</el-radio-button>
                            <el-radio-button label="1" type="success">有效</el-radio-button>
                            <!-- <el-radio-button label="2" type="info">潜在</el-radio-button> -->
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="有效性" v-else>
                        <el-switch
                            v-model="baTable.form.items!.ACTIVE"
                            style="--el-switch-on-color: #13ce66;"
                            active-value="1"
                            inactive-value="0"
                        />
                    </el-form-item>
    
                    <el-form-item label="VTG客户" label-width="100px">
                        <el-switch
                            v-model="baTable.form.items!.VTG"
                            style="--el-switch-on-color: #13ce66;"
                            active-value="1"
                            inactive-value="0"
                        />
                    </el-form-item>
                    <el-form-item label="公共客户" label-width="100px">
                        <el-radio-group v-model="baTable.form.items!.PFLAG">
                            <el-radio-button label="0" type="danger">否</el-radio-button>
                            <el-radio-button label="1" type="success">是</el-radio-button>
                        </el-radio-group>
                    </el-form-item>
                    
                </div>
                <el-row>
                    <el-col :span="10">
                        <FormItem
                            :label="t('corplib.CORP')"
                            type="string"
                            v-model="baTable.form.items!.CORP"
                            prop="CORP"
                            :input-attr="{ placeholder: t('Please input field', { field: t('corplib.CORP') }) ,disabled:baTable.form.operate !== 'add'}"
                        />
                        <FormItem
                            :label="t('corplib.REMIND')"
                            type="string"
                            v-model="baTable.form.items!.REMIND"
                            prop="REMIND"
                            :input-attr="{ placeholder: t('Please input field', { field: t('corplib.REMIND') }) }"
                        />
                        <FormItem
                            :label="t('corplib.CORPNAME')"
                            type="string"
                            v-model="baTable.form.items!.CORPNAME"
                            prop="CORPNAME"
                            :input-attr="{ placeholder: t('Please input field', { field: t('corplib.CORPNAME') }) }"
                        />
                        <FormItem
                            :label="t('corplib.ENAME')"
                            type="string"
                            v-model="baTable.form.items!.ENAME"
                            prop="ENAME"
                            :input-attr="{ placeholder: t('Please input field', { field: t('corplib.ENAME') }) }"
                        />
                        <FormItem
                            :label="t('corplib.ADDRESS')"
                            type="textarea"
                            v-model="baTable.form.items!.ADDRESS"
                            prop="ADDRESS"
                            :input-attr="{ rows: 3, placeholder: t('Please input field', { field: t('corplib.ADDRESS') }) }"
                            @keyup.enter.stop=""
                            @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                        />
                        <FormItem
                            :label="t('corplib.COUNTERCLAIM')"
                            type="textarea"
                            v-model="baTable.form.items!.COUNTERCLAIM"
                            prop="COUNTERCLAIM"
                            :input-attr="{ placeholder: t('Please input field', { field: t('corplib.COUNTERCLAIM') }) }"
                        />
                        <FormItem
                            :label="t('corplib.INSURANCE')"
                            type="textarea"
                            v-model="baTable.form.items!.INSURANCE"
                            prop="INSURANCE"
                            :input-attr="{ placeholder: t('Please input field', { field: t('corplib.INSURANCE') }) }"
                        />
                        <FormItem
                            :label="t('corplib.CORPITEM')"
                            type="textarea"
                            v-model="baTable.form.items!.CORPITEM"
                            prop="CORPITEM"
                            :input-attr="{ placeholder: t('Please input field', { field: t('corplib.CORPITEM') }) }"
                        />
    
    
                    </el-col>
    
                    <el-col :span="7">
                        <FormItem
                            :label="t('corplib.CIQCOD')"
                            type="string"
                            v-model="baTable.form.items!.CIQCOD"
                            prop="CIQCOD"
                            :input-attr="{ placeholder: t('Please input field', { field: t('corplib.CIQCOD') }) }"
                        />
                        <FormItem
                            :label="t('corplib.LINKMAN')"
                            type="string"
                            v-model="baTable.form.items!.LINKMAN"
                            prop="LINKMAN"
                            :input-attr="{ placeholder: t('Please input field', { field: t('corplib.LINKMAN') }) }"
                        />
                        <FormItem
                            :label="t('corplib.POST')"
                            type="string"
                            v-model="baTable.form.items!.POST"
                            prop="POST"
                            :input-attr="{ placeholder: t('Please input field', { field: t('corplib.POST') }) }"
                        />
                        <FormItem
                            :label="t('corplib.TELEPHONE')"
                            type="string"
                            v-model="baTable.form.items!.TELEPHONE"
                            prop="TELEPHONE"
                            :input-attr="{ placeholder: t('Please input field', { field: t('corplib.TELEPHONE') }) }"
                        />
                        <FormItem
                            :label="t('corplib.EMAIL')"
                            type="string"
                            v-model="baTable.form.items!.EMAIL"
                            prop="EMAIL"
                            :input-attr="{ placeholder: t('Please input field', { field: t('corplib.EMAIL') }) }"
                        />
    
                        <FormItem
                            :label="t('corplib.SETTLEMENT')"
                            v-model="baTable.form.items!.SETTLEMENT"
                            prop="SETTLEMENT"
                            type="remoteSelect"
                            :input-attr="{
                                pk: 'code',
                                field: 'codename',
                                'remote-url': codelib + 'paymode',
                                placeholder: t('Click Select'),
                            }"
                        />
                        <FormItem
                            :label="t('corplib.INVOICETYPE')"
                            type="select"
                            v-model="baTable.form.items!.INVOICETYPE"
                            prop="INVOICETYPE"
                            :data="{ content: { '0': '增票', '1': '普票' } }"
                            :input-attr="{ placeholder: t('Please select field', { field: t('corplib.INVOICETYPE') }) }"
                        />
                        <FormItem
                            :label="t('corplib.TAXNO')"
                            type="string"
                            v-model="baTable.form.items!.TAXNO"
                            prop="TAXNO"
                            :input-attr="{ placeholder: t('Please input field', { field: t('corplib.TAXNO') }) }"
                        />
                        <FormItem
                            :label="t('corplib.COMPANYTYPE')"
                            v-model="baTable.form.items!.GSTYPE"
                            prop="GSTYPE"
                            type="remoteSelect"
                            :input-attr="{
                                multiple:true,
                                pk: 'code',
                                field: 'codename',
                                'remote-url': codelib + 'type',
                                placeholder: t('Click Select'),
                            }"
                        />
                    </el-col>
                    <el-col :span="7">
                        <FormItem
                            :label="t('corplib.COUNTRY')"
                            v-model="baTable.form.items!.COUNTRY"
                            prop="COUNTRY"
                            type="remoteSelect"
                            :input-attr="{
                                multiple:true,
                                pk: 'CODE',
                                field: 'CHFNAM',
                                'remote-url': couare +'code?othcod=1',
                                placeholder: t('Click Select'),
                            }"
                        />
                        <FormItem
                            :label="t('corplib.PROVINCE')"
                            v-model="baTable.form.items!.PROVINCE"
                            prop="PROVINCE"
                            type="remoteSelect"
                            :input-attr="{
                                multiple:true,
                                pk: 'CODE',
                                field: 'CHFNAM',
                                'remote-url': couare +'code?othcod=2',
                                placeholder: t('Click Select'),
                            }"
                        />
                        <FormItem
                            :label="t('corplib.AREA')"
                            v-model="baTable.form.items!.AREA"
                            prop="AREA"
                            type="remoteSelect"
                            :input-attr="{
                                multiple:true,
                                pk: 'CODE',
                                field: 'CHFNAM',
                                'remote-url': couare +'code?othcod=3',
                                placeholder: t('Click Select'),
                            }"
                        />
                        <FormItem
                            :label="t('corplib.FAX')"
                            type="string"
                            v-model="baTable.form.items!.FAX"
                            prop="FAX"
                            :input-attr="{ placeholder: t('Please input field', { field: t('corplib.FAX') }) }"
                        />
                        <FormItem
                            :label="t('corplib.DEPT')"
                            v-model="baTable.form.items!.DEPT"
                            prop="DEPT"
                            type="remoteSelect"
                            :input-attr="{
                                pk:'DEPTID',
                                field: 'DEPTNAME',
                                'remote-url':dEPARTMENT +'index',
                                placeholder: t('Click Select'),
                            }"
                        />
                        <FormItem
                            :label="t('corplib.MAXCREDIT')"
                            type="number"
                            prop="MAXCREDIT"
                            v-model.number="baTable.form.items!.MAXCREDIT"
                            :input-attr="{ step: '0.01', placeholder: t('Please input field', { field: t('corplib.MAXCREDIT') }) }"
                        />
                        <FormItem
                            :label="t('corplib.CURRENCY')"
                            v-model="baTable.form.items!.CURRENCY"
                            prop="CURRENCY"
                            type="remoteSelect"
                            :input-attr="{
                                pk: 'MONEYTYPE',
                                field: 'CURNAME',
                                'remote-url': codelib + 'curr',
                                placeholder: t('Click Select'),
                            }"
                        />
                        <!-- 信用等级 -->
                        <FormItem
                            :label="t('corplib.CREADITCLASS')"
                            v-model="baTable.form.items!.CREADITCLASS"
                            prop="CREADITCLASS"
                            type="remoteSelect"
                            :input-attr="{
                                pk: 'code',
                                field: 'code',
                                'remote-url': codelib + 'creditclass',
                                placeholder: t('Click Select'),
                            }"
                        />
                        <FormItem
                            :label="t('corplib.SALER')"
                            v-model="baTable.form.items!.SALER"
                            prop="SALER"
                            type="remoteSelect"
                            :input-attr="{
                                pk: 'nickname',
                                field: 'nickname',
                                'remote-url':authAdmin+'index',
                                placeholder: t('Click Select'),
                            }"
                        />
                        <!-- 操作人 -->
                        <FormItem
                            :label="t('corplib.CZY')"
                            v-model="baTable.form.items!.CZY"
                            prop="CZY"
                            type="remoteSelect"
                            :input-attr="{
                                pk: 'nickname',
                                multiple:true,
                                field: 'nickname',
                                'remote-url':authAdmin+'index',
                                placeholder: t('Click Select'),
                            }"
                        />
    
                    </el-col>
                </el-row>
                <FormItem
                    :label="t('corplib.REMARK')"
                    type="textarea"
                    v-model="baTable.form.items!.REMARK"
                    prop="REMARK"
                    :input-attr="{rows: 4, placeholder: t('Please input field', { field: t('corplib.REMARK') }) }"
                />
                <el-row>
                    <el-col :span="8">
                        <FormItem
                            :label="t('corplib.OP')"
                            type="string"
                            v-model="baTable.form.items!.OP"
                            prop="OP"
                            :input-attr="{ placeholder: '当前操作人',disabled:true }"
                        />
                    </el-col>
                    <el-col :span="8">
                        <FormItem
                            :label="t('corplib.OPTIME')"
                            type="string"
                            v-model="baTable.form.items!.OPTIME"
                            prop="OPTIME"
                            :input-attr="{ placeholder: '当前时间',disabled:true }"
                        />
                    </el-col>
                </el-row>
            </el-form>
        </div>

        <el-dialog
            v-model="editShow"
            title="修改申请"
            width="600px"
        >
            <edit-apply v-if="editShow"  />
        </el-dialog>

    </div>
</template>

<script setup lang="ts">
import { reactive, ref, inject, onMounted } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import { ElForm, ElMessage, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'
import { codelib,authAdmin,dEPARTMENT ,couare} from '/@/api/controllerUrls'
import createAxios from '/@/utils/axios'
import { useRoute } from 'vue-router'
import {corplib} from "/@/api/controllerUrls";
import editApply from './editApply.vue'


const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    CORP: [buildValidatorData({ name: 'required', title: t('corplib.CORP') })],
    REMIND: [buildValidatorData({ name: 'required', title: t('corplib.REMIND') })],
    CORPNAME: [buildValidatorData({ name: 'required', title: t('corplib.CORPNAME') })],
    SUBDT: [buildValidatorData({ name: 'date', message: t('Please enter the correct field', { field: t('corplib.SUBDT') }) })],
    AUDITDT: [buildValidatorData({ name: 'date', message: t('Please enter the correct field', { field: t('corplib.AUDITDT') }) })],
    OPTIME: [buildValidatorData({ name: 'date', message: t('Please enter the correct field', { field: t('corplib.OPTIME') }) })],
})

const activeName = ref('first')
const route = useRoute()
const type = route.name === 'corplib/customer'? 1:2

// console.log(route)
const companyName = inject('companyName') as String

onMounted(()=>{
    baTable.form.items!.type = type
    if(baTable.form.operate == 'add'){
        baTable.form.items!.ACTIVE = '1'
    }
})

const doSave = () => {
    baTable.onSubmit(formRef.value,()=>{
        companyName.value = baTable.form.items!.CORP
        baTable.form.operate = 'edit'
    })
}

const handleTj = ()=>{
    createAxios({
    url:'admin/Corplib/tj',
    data:{
           CORP:baTable.form.items!.CORP,
           corp:baTable.form.items!.CORP,
           TYPE:'1',
           type:'1'
        },
        method:'post'
  }).then(()=>{
    ElMessage.success('提交成功')
    baTable.form.items!.status = '0'
  })
}

defineExpose({
    doSave
})

const editShow = ref(false)




</script>

<style scoped lang="scss">
.ba-operate-form{
    padding-top:0;
    .form-wrapper{
        position: relative;
    }
    .lock-btn{
        position: absolute;
        top:0px;
        left:580px;
        &.supplier{
            left:520px
        }
    }
}
.status-wrapper{
    display: flex;
}
</style>
